package com.mccloud.mini.util;

import cn.hutool.core.text.StrBuilder;
import cn.hutool.crypto.SecureUtil;

import java.util.Map;
import java.util.TreeMap;

/**
 * @Description:抖店参数加密工具类
 * @ClassName: DouDianutil
 * @Author: 宋发准
 * @Date: 2020/8/26 15:07
 * @Version: 1.0
 */
public class DouDianUtil {


    public static String getParamJson(TreeMap<String, String> param) {
        StrBuilder sb = new StrBuilder();
        if (param.size() == 0) {
            sb.append("{");
        } else {
            for (Map.Entry<String, String> entry : param.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                if (sb.length() == 0) {
                    sb.append("{");
                } else {
                    sb.append(",");
                }
                sb.append("\"").append(key).append("\"").append(":").append("\"").append(value).append("\"");
            }
        }
        sb.append("}");
        return sb.toString();
    }

    public static String getSign(String appKey, String appSecret, String method, String paramJson, String now, String v) {
        TreeMap<String, String> map = new TreeMap<>();
        map.put("app_key", appKey);
        map.put("method", method);
        map.put("param_json", paramJson);
        map.put("timestamp", now);
        map.put("v", v);

        StringBuffer sb = new StringBuffer();
        sb.append(appSecret);
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            sb.append(key).append(value);
        }
        sb.append(appSecret);
        return SecureUtil.md5(sb.toString());
    }

}